package com;

import tool.jdbctool;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(name = "ques_update")
public class ques_update1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        ServletContext servletContext=request.getServletContext();
        question question= (com.question) servletContext.getAttribute("key");
        try {
            connection=jdbctool.getconection();

            statement=connection.prepareStatement("update `servlet`.`question`\n" +
                    "set \n" +
                    "  `title` = ?,\n" +
                    "  `A` = ?,\n" +
                    "  `B` = ?,\n" +
                    "  `C` = ?,\n" +
                    "  `D` = ?,\n" +
                    "  `answer` = ?\n" +
                    "where `questionId` = ?;");
            statement.setString(1,request.getParameter("title"));
            statement.setString(2,request.getParameter("A"));
            statement.setString(3,request.getParameter("B"));
            statement.setString(4,request.getParameter("C"));
            statement.setString(5,request.getParameter("D"));
            statement.setString(6,request.getParameter("answer"));
            statement.setInt(7,question.getQuestionid());
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            jdbctool.close(connection,statement,resultSet);
        }
        PrintWriter out=response.getWriter();
        out.write("修改成功！！");
    }
}
